package ihm;

import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.JOptionPane;
import javax.swing.JPanel;


import database.DataBase;
import database.Operation;

public class OperationPanel extends javax.swing.JPanel{
	
	private MainFrame frame;

	private HashMap<String,ArrayList<String>> fgts;
	private Operation op;
	
	private String process, fragmentName, component;
	private DataBase db;
	
	
	private javax.swing.JButton jButton2;
	private javax.swing.JButton jButton3;
	private javax.swing.JButton jButton4;
	private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JComboBox jComboBox3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    
    private DefinedOperationPanel jPanel1;
    
	public OperationPanel(HashMap<String,ArrayList<String>> _fgts, String p, MainFrame f) {
		super(new FlowLayout());
		frame=f;
		this.fgts=new HashMap<String, ArrayList<String>>();
        this.fgts.putAll(_fgts);
    	op=Operation.NO;
    	this.process=p;
    	db=new DataBase(f.getDbFile());
    	jPanel1=new DefinedOperationPanel();
    	initComponents();
	}
	 private void initComponents() {
	        jComboBox1 = new javax.swing.JComboBox(fgts.keySet().toArray());
	        jLabel1 = new javax.swing.JLabel();
	        jLabel2 = new javax.swing.JLabel();
	        jComboBox2 = new javax.swing.JComboBox();
	        jLabel3 = new javax.swing.JLabel();
	        jButton2 = new javax.swing.JButton();
	        jComboBox3 = new javax.swing.JComboBox();
	        jButton3 = new javax.swing.JButton();
	        jButton4 = new javax.swing.JButton();


	        jComboBox1.setEditable(false);
	        jComboBox1.setSelectedIndex(-1);
	        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                jComboBox1ActionPerformed(evt);
	            }
	        });

	        jLabel1.setText("Fragment:");

	        jLabel2.setText("MMMl Element/Relationship:");
	        
	        //jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
	        jComboBox2.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                jComboBox2ActionPerformed(evt);
	            }
	        });

	        jLabel3.setText("Operation:");

	        jButton2.setIcon(new javax.swing.ImageIcon("C:\\Users\\Noelie\\Pictures\\valider2.png")); // NOI18N
	        jButton2.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                jButton2ActionPerformed(evt);
	            }
	        });
	        
	        jComboBox3.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                jComboBox3ActionPerformed(evt);
	            }
	        });

	        jButton3.setIcon(new javax.swing.ImageIcon("C:\\Users\\Noelie\\Pictures\\back.gif")); // NOI18N
	        jButton3.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                jButton3ActionPerformed(evt);
	            }
	        });

	        jButton4.setIcon(new javax.swing.ImageIcon("C:\\Users\\Noelie\\Pictures\\add.gif")); // NOI18N
	        jButton4.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                jButton4ActionPerformed(evt);
	            }
	        });

	        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(this);
	        this.setLayout(jPanel1Layout);
	        jPanel1Layout.setHorizontalGroup(
	            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
	                .addGap(20, 20, 20)
	                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE)
	                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 172, Short.MAX_VALUE)
	                    .addComponent(jLabel3)
	                    .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE))
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
	                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
	                    .addComponent(jComboBox2, javax.swing.GroupLayout.Alignment.LEADING, 0, 228, Short.MAX_VALUE)
	                    .addComponent(jComboBox1, javax.swing.GroupLayout.Alignment.LEADING, 0, 228, Short.MAX_VALUE)
	                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
	                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
	                                .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
	                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
	                                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE))
	                            .addComponent(jComboBox3, 0, 228, Short.MAX_VALUE))))
	                .addGap(40, 40, 40)
	                .addComponent(jPanel1,javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)        
	                .addContainerGap())
	        );
	        
	        jPanel1Layout.setVerticalGroup(
	        	jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	        		.addGap(65, 65, 65)
		            .addGroup(jPanel1Layout.createSequentialGroup()
		                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
		                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
		                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
		                .addGap(18, 18, 18)
		                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
		                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
		                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
		                .addGap(18, 18, 18)
		                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
		                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
		                    .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
		                .addGap(92, 92, 92)
		                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
		                    .addComponent(jButton2)
		                    .addComponent(jButton4)
		                    .addComponent(jButton3))
		                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		            .addGap(50, 50, 50)
		            .addComponent(jPanel1,javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
	        );
	    }


	    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
	        String choice=(String) jComboBox1.getSelectedItem();
	        fragmentName=choice;
	        jComboBox2.setEditable(false);
	    	jComboBox2.setSelectedIndex(-1);
	    	if(fgts.get(choice).size()>0){
	    		ArrayList<String> comp=new ArrayList<String>();
	    		comp.addAll(fgts.get(choice));
	    		jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(comp.toArray()));
//	    		for(int i=0;i<comp.size();i++)
//	    			jComboBox2.addItem(comp.get(i));
	    	}	
	    	jComboBox2.setSelectedIndex(-1);
	    	jComboBox3.setSelectedIndex(-1);
	    	jPanel1.updateDefinedOperation(db.getFragment(process, fragmentName));
	    }                                          

	    private void jComboBox2ActionPerformed(java.awt.event.ActionEvent evt) {  
	    	String choiceComp=(String) jComboBox2.getSelectedItem();
	    	//System.out.println("jcombobox 2 choice : "+choiceComp );
	    	this.component=choiceComp;
	    	if(choiceComp!=null){
		    	//System.out.println("jcombobox 2 ");
		    	jComboBox3.setEditable(false);
		        jComboBox3.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "D", "R", "Q", "QR", "RF", "RFR" }));
		        jComboBox3.setSelectedIndex(-1);
	    	}
	    } 
	    
	    private void jComboBox3ActionPerformed(java.awt.event.ActionEvent evt) {
	    	String choiceOp=(String) jComboBox3.getSelectedItem();
	    	if(choiceOp!=null){
	    		op=getOperation(choiceOp);
	    		//System.out.println("operation :  "+op);
	    	}
	    	
	    }
	    
	    private Operation getOperation(String opName) {
	    	if(opName==null){
	    		return null;
	    	}
	    	else{
			 if(opName.equals(Operation.D.toString()))
				 return Operation.D;
			 if(opName.equals(Operation.Q.toString()))
				 return Operation.Q;
			 if(opName.equals(Operation.R.toString()))
				 return Operation.R;
			 if(opName.equals(Operation.QR.toString()))
				 return Operation.QR;
			 if(opName.equals(Operation.RF.toString()))
				 return Operation.RF;
			 if(opName.equals(Operation.RFR.toString()))
				 return Operation.RFR;
			 if(opName.equals(Operation.NO.toString()))
				 return Operation.NO;
	    	}
			return null;
		}
	    
	    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
			frame.setTitleToMetricsPanel();
			CardLayout cl = (CardLayout)(frame.getPan().getLayout());
			cl.show(frame.getPan(), "metricPanel");
	        
	    }

	    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
	    	frame.setTitleToFragmentPanel();
	    	CardLayout cl = (CardLayout)(frame.getPan().getLayout());	
			cl.show(frame.getPan(), "fragmentPanel");
			
	    }

	    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
	    	fragmentName=(String) jComboBox1.getSelectedItem();
	    	component=(String) jComboBox2.getSelectedItem();
	    	op=getOperation((String)jComboBox3.getSelectedItem());
	    	System.out.println(component+"  operation : "+op);
	    	if(fragmentName==null || component==null || op==null){
	    		JOptionPane.showMessageDialog(null, "Complete all field",
	                    "Warning", JOptionPane.ERROR_MESSAGE);
	    	}
	    	else{
	    		db.addOperation(db.getFragment(process,fragmentName), component, op);
	    	}
	        jPanel1.updateDefinedOperation(db.getFragment(process, fragmentName));
	    }

		public void setOp(Operation op) {
			this.op = op;
		}

		public Operation getOp() {
			return op;
		}

}
